package rickyGame.net.handler
{
	import flash.events.Event;
	import flash.utils.getTimer;
	
	import org.hamcrest.mxml.collection.InArray;
	
	import ricky.game.Actor2D;
	
	import rickyGame.controller.Controller;
	import rickyGame.game.GameState;
	import rickyGame.game.scene.SceneManager;
	import rickyGame.model.*;
	import rickyGame.model.config.*;
	import rickyGame.net.msg.SvrMsg;
	import rickyGame.ui.UIManager;
	import rickyGame.ui.login.LoginView;

	public class Handler
	{
		public function Handler()
		{
		}
		
		/**
		 * 接收提示消息
		 */
		public function showTips(obj:Object):void
		{
			trace(obj.arg);	
		}
		
		/**
		 * 接收网络ping包
		 */
		public function ping(obj:Object):void
		{
			if(isPingSend)
			{
				reveicePingTime=getTimer();
				MsgHelper.netDelay=reveicePingTime-sendPingTime;
				isPingSend=false;
				var act:Actor2D=Controller.instance.actor;
				if(act)
					act.netDelay=MsgHelper.netDelay;
				
				var smsg:SvrMsg=new SvrMsg("Room",5,"delay");
				MsgHelper.sendMsgToS(smsg);
				//				Debug.traceObj("网络延时:"+MsgHelper.netDelay,"ping");	
			}
		}

		/**
		 * 发送ping包
		 */
		public static function sendPing():void
		{
			if(!isPingSend)
			{
				sendPingTime=getTimer();
				var obj:SvrMsg=new SvrMsg("Pub",0,{"delay":MsgHelper.netDelay});
				MsgHelper.sendMsgToS(obj);
				isPingSend=true;
			}
		}
		
		/**
		 * 创建英雄
		 */
		public function showLogin(msg:Object):void
		{
			trace("开始创建英雄");
			var p:LoginView=new LoginView();
			p.initialize();
			p.addEventListener("createSure",onCreated);
			
			UIManager.inst.uiLayer.addChild(p);
		}
		
		private function onCreated(e:Event):void
		{
			var p:LoginView=e.target as LoginView;
			var hreoName:String=p.getHeroName();
			var unitID:String=p.choose.data.unitID;
			p.dispose();
			
			var unit:Unit=ConfigUnitHelper.getHeroConfig(unitID);

			var obj:SvrMsg=new SvrMsg("RoomAu",NaN,{"info":unit});

			User.heroUnit=unit;

			sendMTS(obj);
		}
		
		public function validated(msg:Object):void
		{
//			UIManager.inst.showHall();
			var smsg:SvrMsg=new SvrMsg("Pub",10);//得到房间列表
			MsgHelper.sendMsgToS(smsg);
		}
		
		/**
		 * 处理act消息
		 */
		public function act(msg:Object):void
		{
			
			var args:Object=msg.arg;
			var name:String=args.name;
			var types:int=args.type;
			var par:*=args.args;
			var sm:SceneManager=GameState.inst.sceneManager;
			var actor:Actor2D=sm.getActorByName(name);
			
			if(actor)
			{
				actor.processAct(types,par);
			}
		}
		
		public static var sendPingTime:int=1;
		public static var reveicePingTime:int=1;
		public static var pingDelay:int=1;
		/** ping命令发送中  */
		public static var isPingSend:Boolean=false;
//		/**
//		 * send message to server
//		 */
//		public static function sendMTS(obj:Object):void
//		{
//			SocketProxy.send(obj["arg"]);
//		}
	}
}